package org.crate.query.api;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import static org.apache.commons.dbutils.DbUtils.close;

/**
 * Created by huangfei on 2018/9/20.
 */
public class DBPoolConnection {
    private static DBPoolConnection dbPoolConnection = null;
    private static DruidDataSource druidDataSource = null;

    static {
        Properties properties = loadPropertiesFile();
        System.out.println(properties);
        try {
            druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties); // DruidDataSrouce工厂模式
            System.out.println("success druidDataSource=" + druidDataSource);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 数据库连接池单例
     * 
     * @return
     */
    public static synchronized DBPoolConnection getInstance() {
        if (null == dbPoolConnection) {
            dbPoolConnection = new DBPoolConnection();
        }
        return dbPoolConnection;
    }

    /**
     * 返回druid数据库连接
     * 
     * @return
     * @throws SQLException
     */
    public DruidPooledConnection getConnection() throws SQLException {
        return druidDataSource.getConnection();
    }

    static File dbConfigfile() throws FileNotFoundException {
        File file = null;

        try {
            URL url = DBPoolConnection.class.getClassLoader().getResource("db_server.properties");
            if (url != null) {
                file = new File(url.getFile());
                if (file != null && file.exists()) {
                    return file;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        file = new File("/home/wenjun/config/db/db_server.properties");
        if (file.exists()) {
            return file;
        }

        throw new FileNotFoundException("file db_server.properties not found exception");
    }

    /**
     * @return Properties对象
     */
    private static Properties loadPropertiesFile() {
        InputStream inputStream = null;
        Properties p = null;
        try {
            File file = dbConfigfile();
            System.out.println("file=" + file);

            inputStream = new FileInputStream(file);
            p = new Properties();
            p.load(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != inputStream) {
                    inputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        System.out.println(p);
        return p;
    }

    public static void main(String[] args) throws SQLException {
        for (int i = 0; i < 10; i++) {
            Connection conn = DBPoolConnection.getInstance().getConnection();
            System.out.println(conn.isClosed());
            close(conn);
        }
    }

}
